package com.qiangqiang.taskuser.mapper.provider;

import com.qiangqiang.taskuser.entity.UserInfo;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


public class UserMapperProvider extends BaseProvider {


    /**
     * @param [map]
     * @return java.lang.String
     * @author sky
     * @date 2018/7/14
     * @describe: 将插入和更新写在一起
     */
    public String upSert(final Map<String, Object> map) {
        UserInfo user = (UserInfo) map.get("user");
        StringBuffer sb = new StringBuffer(UPSERT).append(UPDATE);
        if (user.getNickName() != null) {
            sb.append("nick_name=#{user.nickName},");
        }
        if (user.getSex() != null) {
            sb.append("sex=#{user.sex},");
        }
        if (user.getSchool() != null) {
            sb.append("school=#{user.school},");
        }
        if (user.getCampus() != null) {
            sb.append("campus=#{user.campus},");
        }
        if (user.getDormitory() != null) {
            sb.append("dormitory=#{user.dormitory},");
        }
        if (user.getHeadImgUrl() != null) {
            sb.append("head_img_url=#{user.headImgUrl},");
        }
        if (user.getUpdateTime() != null) {
            sb.append("update_time=#{user.updateTime},");
        }
        if (user.getEmail() != null) {
            sb.append("email=#{user.email},");
        }
        if (user.getPassword() != null) {
            sb.append("password=#{user.password},");
        }
        if (user.getPhone() != null && !"".equals(user.getPhone().trim())) {
            sb.append("phone=#{user.phone}");
        }
        return sb.toString();
    }

    public String queryAllUser(final Map<String, Object> map) {
        final int page = (int) map.get("page");
        final int pageSize = (int) map.get("pageSize");

        SQL sql = new SQL() {
            {
                SELECT("*");
                FROM(USER);
                //这里写条件
            }
        };
        StringBuffer sb = new StringBuffer(sql.toString());
        sb.append(getLimit(page, pageSize));
        return sb.toString();
    }

    public String queryNamesByIds(final Map<String, Object> map) {
        List<Long> ids = (List<Long>) map.get("userIds");

        StringBuffer sb = new StringBuffer(GET_NICK_NAME);
        sb.append(String.join(",", ids.stream().
                map(String::valueOf).collect(Collectors.toList()))).append(")");
        return sb.toString();
    }

    public String queryNickNameDTOByIds(final Map<String, Object> map) {
        List<Long> ids = (List<Long>) map.get("userIds");
        StringBuffer sb = new StringBuffer(GET_NICK_NAME_DTO);
        sb.append(String.join(",", ids.stream().
                map(String::valueOf).collect(Collectors.toList()))).append(")");
        return sb.toString();
    }
}
